<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>jquery的底层方法</title>
    <!-- 引入jquery -->
    <script src='../day14/jquery.js'></script>
    <!-- 引入moment -->
    <script src='../day09/moment.js'></script>
    <script src='./qs.js'></script>
    <style>
        body {
            margin: 0;
        }
        .btn {
            width:60px;
            height:30px;
            border-radius: 8px;
            background-color: #1890ff;
            color: #fff;
            text-align:center;
            line-height: 30px;
            font-size: 11px;
            margin-bottom: 1em;
        }
        .dilog { 
            width: 800px;
            height:600px;
            background-color: #fff;
            position:absolute;
            top: 3em;
            left: 50%;
            margin-left: -400px;
            z-index: 3;
            opacity:1;
            border-radius: 8px;
        }
        .main {
            width: 100%;
            min-height: 800px;
            background-color: rgba(0, 0, 0, 0.5);
            position:absolute;
            top: 0;
            display:none;
        }
    </style>
</head>
<body>
    <div class="btn" onclick='toAdd()'>新增</div>
    <table border=1 width=100% cellspacing='0' cellpadding='0'> 
        <thead>
            <tr>
                <th>编号</th>
                <th>标题</th>
                <th>状态</th>
                <th>发布时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td></td>
                <td></td>
                <td></td>
                <td></td>
                <td>
                    <a href="">删除</a>
                    <a href="">编辑</a>
                </td>
            </tr>
        </tbody>
    </table>
    <!-- 模态框开始 -->
    <div class="main">
        <div class="dilog">
            <p>新增用户</p>
            <div class="form">
                <p>用户名：<input type="text"></p>
                <p>密码：<input type="password"></p>
            </div>
            <div class="footer">
                <span href="" onclick="cancelHandler()">取消</span>
                <span href="" onclick='submitHandler()'>确定</span>
            </div>
        </div>
    </div>
    <!-- 模态框结束 -->
</body>
<script>
    var baseUrl = 'http://47.93.206.13:8002'
    //查询所有资讯分类信息
    function findAllCategory(){
        //数据交互
        $.ajax({
            type: "GET",
            url: baseUrl+'/index/pageQueryArticles',
            data:'page=1&pageSize=20',
            success:function(res){
                if(res.status == 200){
                    //拿到实际数据
                    var r = res.data.list
                    //遍历数据
                    r.forEach(function(item){
                        //打印数组的每一项
                        // console.log(item)
                       //将数据展示到页面上 
                       //克隆tr
                        var newTr = $('table tbody tr:first').clone(true);
                        //给每个td给内容
                        newTr.children('td').eq(0).html(item.id)
                        newTr.children('td').eq(1).html(item.title);
                        newTr.children('td').eq(2).html(item.status)
                        newTr.children('td').eq(3).html(moment(item.publishTime).format('YYYY-MM-DD hh:mm:ss'))
                        //将克隆出来的tr追加到tbody中
                        $('table tbody').append(newTr)
                    })
                    $('table tbody tr').filter(function(index){
                        return index == 0
                    }).remove()
                }else {
                    alert(res.message);
                }
            }
        })
    }
    findAllCategory()
    //去添加
    function toAdd(){
        $('.main').css({'display' : 'block'})
    }
    //取消
    function cancelHandler() {
        $('.main').css({'display' : 'none'});
    }
    //提交
    function submitHandler(){
        //获取用户输入的信息
        var form = {
            username:$('input[type=text]').val(),
            password:$('input[type=password]').val()
        }
        //设置请求头Authorization
        $.ajaxSetup({
            headers:{
                'Authorization':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJNalU9Iiwic3ViIjoiYWRtaW4xIiwiaXNzIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJpYXQiOjE2MzEyNDY2MTMsImF1ZCI6InJlc3RhcGl1c2VyIiwiZXhwIjoxNjMxNDE5NDEzLCJuYmYiOjE2MzEyNDY2MTN9.mj1uVrydC7OYc8I_gEWlFLiSNEfFpuETrO9ERIKOqcY'
            }
        })
        //与后台进行交互
        $.ajax({
            //请求方式
            type: 'POST',
            //请求地址
            url: baseUrl+'/baseUser/saveOrUpdate',
            //请求参数
            data:form,
            //请求成功的回掉函数
            success:function(data){
                console.log(data)
            }
        })
    }
</script>
</html>



